/**
 *  uart.c - A serial driver sample for device driver framwork.
 *
 *  Copyright (C) 2008-2010 ZhangHu
 *  All rights reserved.
 *  E-MAIL: anmnmnly@gmail.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "include/types.h"
#include "include/mem.h"
#include "include/sem.h"
#include "include/irq.h"
#include "drivers/chdev.h"
#include "drivers/uart.h"

#include "devs.h" /* Depends on CPU and its peripheral devices. */


extern void init_uart_args(struct uart_mode Mode);
extern int32 uart_send_byte(char_t ch);

static uart_t puart0;



static uint32 uart_rev_str(char_t *buf, int32 len)
{
    char_t *str = buf;
    bool_t err = FALSE;

    if(puart0.prevbuf == NULL) {
        return(0);
    }

    while(len--) {
        if(bufGetUsed(puart0.prevbuf)) {
            *str = (char_t)bufRev(puart0.prevbuf, &err);
            if(err == TRUE) {
                str++;
            }
        }
    }

    return (str - buf);
}



void uart_write(char_t value)
{
    if(puart0.prevbuf != NULL) {
        bufSend(puart0.prevbuf, value);
    }
}



static int32 uart0_init(struct uart_mode Mode)
{
    buf_t *pbufr;

    if((pbufr = bufInit(32)) == NULL) {
        return FALSE;
    }

    puart0.prevbuf = pbufr;
    puart0.psndbuf = NULL;

    init_uart_args(Mode);
    return TRUE;
}



static int32 uart0_open(void)
{
    return(TRUE);
}



static int32 uart0_close(void)
{
    return(TRUE);
}



static int32 uart0_write(const char_t *buf, uint32 len, loff_t loff)
{
    int32 i;

    if(buf == NULL) {
        return 0;
    }

    for(i=0;i<len;i++) {
        if(uart_send_byte(buf[i]) != TRUE) {
            break;
        }
    }

    return i;
}



static int32 uart0_read(char_t *buf, uint32 len, loff_t loff)
{
    if(buf == NULL) {
        return 0;
    }

    return uart_rev_str(buf, len);
}



static int32 uart0_ioctl(uint32 cmd, void *arg)
{
    struct uart_mode *p;
    struct uart_mode mode;

    if(cmd == UART_CMD_SETMODE) {
        if(arg == NULL) {
            return(-1);
        }

        p = (struct uart_mode*)arg;
        mode.Baud = p->Baud;
        mode.DataLen = p->DataLen;
        mode.Parity = p->Parity;
        mode.StopLen = p->StopLen;
        init_uart_args(mode);
    } else if(cmd == UART_CMD_RESET) {
        bufClear(puart0.prevbuf);
    }

    return(0);
}



static void uart0_release(void)
{
    if(puart0.prevbuf != NULL) {
        bufDel(puart0.prevbuf);
    }

    if(puart0.psndbuf != NULL) {
        bufDel(puart0.psndbuf);
    }
}



struct chdev_operations uart0_op = {
    uart0_open,
    uart0_close,
    uart0_write,
    uart0_read,
    uart0_ioctl,
    uart0_release,
};



void uart_register(void)
{
    struct uart_mode mode;

    mode.DataLen = 8;
    mode.StopLen = 1;
    mode.Parity = UART_PARITY_NONE;
    mode.Baud = 115200;

    uart0_init(mode);
    chdevRegister(UART_ID,
        UART0_ID,
        UART0,
        1,
        &uart0_op);
}

